// pages/owner/index.js
const APP = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        //固定设置
        sysStateBarHeight: APP.globalData.statebar_height,
        //逻辑变量
        dataUserInfo:{
            avatar:'/asset/img/empty/icon_portait_gray.png',
            realname:'登 录',
            remark:'',
        },
        isLogin:false,
        isLookMessage:false,//查看消息
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let _this = this;
        //控制权限
        APP.appauth.setAuthSetting();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let _this = this;
        APP._checkOnline(null,null,function(){
            _this.refreshLoginState();
        });
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    // ====================【逻辑区域】====================

    //登录状态
    refreshLoginState(){
        if(APP.appauth.checkAuth()){
            let tmpNickname = APP.appauth.getAuthNickname();
            let tmpAvatar = APP.appauth.getAuthPortrait();
            let lookPermission = APP.appauth.getAuthPermission();
            this.setData({
                isLogin:true,
                isLookMessage:(lookPermission.lookMessage==1),
                ['dataUserInfo.realname']:(tmpNickname?tmpNickname:'   '),
                ['dataUserInfo.avatar']:(tmpAvatar?tmpAvatar:'/asset/img/empty/icon_portait_blue.png'),
            });
        }else{
            this.setData({
                isLogin:false,
                isLookMessage:false,
                dataUserInfo:{
                    avatar:'/asset/img/empty/icon_portait_gray.png',
                    realname:'登 录',
                    remark:'',
                },
            });
        }
    },

    isHasPermission: function(){
        return (parseInt(this.data.dataUserInfo.user_type) === 1);
    },

    clickEvent: function (event) {
        let _this = this, dataset = event.currentTarget.dataset;
        let tid = dataset.id, path = '';

        if(tid != 'about'){
            if(!APP._checkLogin()){
                return;
            }
        }

        switch (tid) {
            case 'personal':
                path = '/pages/owner/sub/personal-info';
            break;
            case 'collection':
                // path = '/pages/owner/sub/collection-list';
                path = '/pages/owner/sub/list';
            break;
            case 'message':
                path = '/pages/owner/sub/message-list';
            break;
            case 'list':
                path = '/pages/owner/sub/list';
            break;
            case 'tablist':
                path = '/pages/owner/sub/tab-list';
            break;
            case 'webpage':
                path = '/pages/common/browser?param=' + APP.apptool.encodeJsonStr({
                    title:'站点网页',url:APP._weburl('/Index/index'),
                });
                break;
            case 'about':
                path = '/pages/owner/sub/about';
            break;
            default:
                APP.apptool.showToastInfo('未找到ID编号为[' + tid + ']的控件，请检查！');
            break;
        }
        //跳转
        if(path.length > 0){
            wx.navigateTo({
                url: path,
            });
        }else{
            APP.apptool.showToastInfo(APP.globalData.disabled_operate_tip);
        }
    },
    //加载图片
    bindImageError: function (eventhandle) {
        APP.apptool.bindImageError(eventhandle, this);
    },
    //安全退出
    bindExit(e){
        let _this = this;
        APP.apptool.showModalConfirm('提示','确认要退出吗？','取消','确认',function(confirm, cancel){
            if(confirm){
                APP.appauth.clearUserCacheData();
                APP.apptool.showToastInfo('已退出登录！');
                _this.refreshLoginState();
            }
        });
    }
})